/*************************************************
* Title：TinyToolKit
* Author：Opportunity 
* Describe：绘制本地化管理器的窗口
* CreateTime：2020-07-07 15:52:24
* Version：1.0
* Modify Recorder：
*************************************************/

using System;
using TinyToolKit;
using TinyToolKit.Localization;
using UnityEditor;
using UnityEngine;
using System.Collections.Generic;

namespace TinyToolKitEditor
{
    public class DrawWindowLocalization : DrawWindowBase
    {
        private TLocalizationManager _localizationManager;
        private SerializedObject _serializedObject;
        
        private SerializedProperty _localizationPathsProperty;
        private TinyToolSettingAsset _toolSetting;

        public override void InitWindow(ScriptableObject setting) 
        {
            _toolSetting = (TinyToolSettingAsset) setting;
            _serializedObject = new SerializedObject(setting);
            _localizationPathsProperty = _serializedObject.FindProperty("localizationPaths");
        }

        protected override void DrawInternalMainGUI() 
        {
            _serializedObject.Update();
            
            EditorGUI.BeginDisabledGroup(EditorApplication.isPlaying);
            _toolSetting.localizationPathType = (LocalizationPathType)TinyToolWindowUtil.DrawEnumPopup("配置表加载路径",_toolSetting.localizationPathType);
            if (_toolSetting.localizationPathType != LocalizationPathType.Custom)
            {
                EditorGUILayout.BeginVertical(EditorStyles.helpBox);
                EditorGUILayout.PropertyField(_localizationPathsProperty, new GUIContent("本地化配置表路径"), true);
                EditorGUILayout.EndVertical();
            }
            else 
            {
                EditorGUILayout.HelpBox("需要调用本地化管理器的 SetLanTranslates() 方法来设置多国语言的配置表", MessageType.Warning);
            }
            EditorGUI.EndDisabledGroup();
            TinyToolWindowUtil.DrawDefaultEditorGUI();

            if (EditorApplication.isPlaying && TinyTool.Instance.HasManager<TLocalizationManager>())
            {
                if (_localizationManager == null) _localizationManager = TinyTool.Instance.GetManager<TLocalizationManager>();

                Language lan = _localizationManager.Language;
                EditorGUILayout.Space();
                EditorGUILayout.LabelField("当前语言：", Enum.GetName(typeof(Language), lan), EditorStyles.boldLabel);

                Dictionary<string, string> trans = _localizationManager.CurLanTranslation();
                if (trans != null && trans.Count > 0)
                {
                    EditorGUILayout.BeginVertical(EditorStyles.helpBox);
                    TinyToolWindowUtil.DrawTableHead("关键字", "翻译");
                    foreach (KeyValuePair<string, string> item in trans)
                    {
                        EditorGUILayout.LabelField(item.Key, item.Value);
                    }
                    TinyToolWindowUtil.DrawTableTail("总计：", _localizationManager.transCount.ToString());
                    EditorGUILayout.EndVertical();
                }
            }
            
            _serializedObject.ApplyModifiedProperties();
        }
    }
}
